<?php


namespace App\Http\Controllers\Customer\Warehouse;


use App\Http\Controllers\Controller;
use App\Http\Requests\Customer\Warehouse\Receiving\StoreRequest;
use App\Http\Requests\Customer\Warehouse\Receiving\UpdateRequest;
use App\Http\Searchs\Customer\Warehouse\Receiving\IndexSearch;
use App\Repositories\Receiving;
use Illuminate\Support\Facades\DB;

class ReceivingController extends Controller
{
    /**
     * @var Receiving
     * @author luffyzhao@vip.126.com
     */
    private $receiving;

    /**
     * Receiving constructor.
     * @param Receiving $receiving
     * @author luffyzhao@vip.126.com
     */
    public function __construct(Receiving $receiving)
    {
        $this->receiving = $receiving;
    }

    /**
     * @param IndexSearch $search
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function index(IndexSearch $search)
    {
        return $this->response(
            $this->receiving->paginate($search->toArray())
        );
    }

    /**
     * @param StoreRequest $request
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function store(StoreRequest $request)
    {
        return $this->response(
            DB::transaction(function () use ($request) {
                $this->receiving->create($request->only([
                    'district', 'files', 'packing', 'piece', 'products', 'transport', 'type', 'weight', 'customer_id', 'remark'
                ]));
            })
        );
    }

    /**
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function edit($id)
    {
        return $this->response(
            $this->receiving->edit($id)
        );
    }

    /**
     * @param UpdateRequest $request
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     * @throws \Throwable
     * @author luffyzhao@vip.126.com
     */
    public function update(UpdateRequest $request, $id)
    {
        return $this->response(
            DB::transaction(function () use ($id, $request) {
                $this->receiving->update($id, $request->only(
                    'district', 'files', 'packing', 'piece', 'products', 'transport', 'type', 'weight', 'customer_id', 'remark'
                ));
            })
        );
    }

    /**
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     * @author: luffyzhao@vip.126.com
     * @datetime: 2019/3/27 19:21
     * @throws \Exception
     */
    public function destroy($id){
        return $this->response(
            DB::transaction(function () use ($id) {
                $this->receiving->delete($id);
            })
        );
    }

    /**
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function confirm($id){
        return $this->response(
            DB::transaction(function () use ($id) {
                $this->receiving->confirm($id);
            })
        );
    }

    /**
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function show($id){
        return $this->response(
            $this->receiving->show($id)
        );
    }
}